<template>
  <view>
      <view v-if="message.length !== 0">
        <view class="" v-for="(item,index) in message" :key="index">
          <view class="">
            <u-swipe-action>
              <u-swipe-action-item :options="options" autoClose="true" @click="deleteMessage(item.id)">
                <user-message :postMessage="item"></user-message>
              </u-swipe-action-item>
            </u-swipe-action>
          </view>
        </view>
      </view>
      <view class="nullimage" v-else>
        <image src="https://s2.loli.net/2022/05/05/r2qiah46pZPYK1O.png" mode=""></image>
        <text>空空如也</text>
      </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //上拉刷新
        total: 0,
        pagenum: 1,
        pagesize: 15,
        flag: false,
        
        message: [],
        serialNumber: '',
        options: [{
           text: '删除',
           style: {
              backgroundColor: '#FC445E',
            }
        }]
      };
    },
    methods: {
      deleteMessage(id) {
        uni.$http.post('/api/inbox/delete/' + id).catch(err => uni.$showMsg('网络错误！'))
        this.message = []
        this.getNews()
      },
      async getNews() {
        this.flag = true
        const {data:{data:{info:res}},data:rcode} = await uni.$http.get('/api/inbox/checkOut/' + this.pagenum).catch(err => uni.$showMsg('网络错误！'))
        if(rcode.code !== 200) return uni.$showMsg()
        this.message = [...this.message,...res.records]
        this.flag = false
        this.total = res.total
        this.pagesize = res.size
      },
      gotoPostItem(e) {
        this.serialNumber = e.info.match(/\d+/g)
        uni.navigateTo({
          url: '../postitem/postitem?id=' + this.serialNumber[0]
        })
      },
      onReachBottom() {
        if(this.pagenum * this.pagesize >= this.total) {
          uni.$showMsg('已经到底了哦~')
          return
        }
        console.log('触发上拉触底刷新')
        if(this.flag) return
        uni.showLoading({
          title: '加载中...'
        })
        this.pagenum += 1
        this.getNews()
      },
    },
    onPullDownRefresh() {
      let timer = null 
      this.message = []
      this.serialNumber = ''
      this.getNews()
      clearTimeout(timer)
      timer = setTimeout(() => {
          uni.stopPullDownRefresh()  
      },1000)
    },
    onShow() {
      this.message = []
      this.serialNumber = ''
      this.pagenum = 1
      this.getNews()
    }
  }
</script>

<style lang="scss">

</style>
